package com.atguigu.cloud.controller;

import com.atguigu.cloud.apis.PayFeignApi;
import com.atguigu.cloud.entities.PayDTO;
import com.atguigu.cloud.resp.ResultData;

import com.atguigu.cloud.resp.ReturnCodeEnum;
import io.github.resilience4j.bulkhead.annotation.Bulkhead;
import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker;
import io.github.resilience4j.ratelimiter.annotation.RateLimiter;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;


/**
 * ClassName: OrderController
 * Package: com.atguigu.cloud.controller
 * Description:
 * Author: 雷建国
 * Create: 2024/3/11 15:03
 * Version: 1.0
 */
@RestController
@Slf4j
public class OrderController {
    @Resource
    private PayFeignApi payFeignApi;

    @PostMapping("/feign/pay/add")
    @CircuitBreaker(name = "cloud-gateway", fallbackMethod = "myCircuitFallback") //创建断路器
    @Bulkhead(name = "cloud-gateway",fallbackMethod = "myBulkheadFallback",type = Bulkhead.Type.SEMAPHORE)
    @RateLimiter(name = "cloud-gateway",fallbackMethod = "myRateLimitFallback")
    public ResultData addOrder(@RequestBody PayDTO payDTO) {
        return payFeignApi.addPay(payDTO);
    }

    @DeleteMapping(value = "/feign/pay/del/{id}")
    @CircuitBreaker(name = "cloud-gateway", fallbackMethod = "myCircuitFallback") //创建断路器
    @Bulkhead(name = "cloud-gateway",fallbackMethod = "myBulkheadFallback",type = Bulkhead.Type.SEMAPHORE)
    @RateLimiter(name = "cloud-gateway",fallbackMethod = "myRateLimitFallback")
    public ResultData deletePay(@PathVariable("id") Integer id) {
        return payFeignApi.deletePay(id);

    }

    @PutMapping(value = "/feign/pay/update")
    @CircuitBreaker(name = "cloud-gateway", fallbackMethod = "myCircuitFallback") //创建断路器
    @Bulkhead(name = "cloud-gateway",fallbackMethod = "myBulkheadFallback",type = Bulkhead.Type.SEMAPHORE)
    @RateLimiter(name = "cloud-gateway",fallbackMethod = "myRateLimitFallback")
    public ResultData updatePay(@RequestBody PayDTO payDTO){
        return payFeignApi.updatePay(payDTO);
    }

    @GetMapping(value = "/feign/pay/get/{id}")
    @CircuitBreaker(name = "cloud-gateway", fallbackMethod = "myCircuitFallback") //创建断路器
    @Bulkhead(name = "cloud-gateway",fallbackMethod = "myBulkheadFallback",type = Bulkhead.Type.SEMAPHORE)
    @RateLimiter(name = "cloud-gateway",fallbackMethod = "myRateLimitFallback")
    public ResultData getById(@PathVariable("id") Integer id){
        return payFeignApi.getById(id);
    }

    /**
     * 临时的服务降级处理兜底方法
     *
     */
    public ResultData myCircuitFallback(Throwable throwable) {
//        return "myCircuitFallback，系统繁忙，请稍后再试-----/(ㄒoㄒ)/~~";
        return ResultData.fail(ReturnCodeEnum.RC201.getCode(),ReturnCodeEnum.RC201.getMessage());
    }

    /**
     * 临时的隔离处理兜底方法
     */
    public ResultData myBulkheadFallback(Throwable throwable)
    {
//        return "myBulkheadFallback，隔板超出最大数量限制，系统繁忙，请稍后再试-----/(ㄒoㄒ)/~~";
        return ResultData.fail(ReturnCodeEnum.RC202.getCode(),ReturnCodeEnum.RC202.getMessage());
    }

    /**
     * 临时的限流处理兜底方法
     */
    public ResultData myRateLimitFallback(Throwable throwable)
    {
        return ResultData.fail(ReturnCodeEnum.RC203.getCode(),ReturnCodeEnum.RC203.getMessage());
    }
}
